package org.cloud.admin.auth.service.impl;


import java.util.ArrayList;
import java.util.List;

import org.cloud.admin.auth.service.SysMenuService;
import org.cloud.admin.auth.service.SysRoleMenuService;
import org.cloud.admin.auth.service.SysUserService;
import org.cloud.common.util.MapUtils;
import org.cloud.module.common.constant.AdminConstants;
import org.cloud.module.common.dao.SysMenuDao;
import org.cloud.module.common.entity.SysMenuEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


@Service("sysMenuService")
public class SysMenuServiceImpl extends ServiceImpl<SysMenuDao, SysMenuEntity> implements SysMenuService {
	
	@Autowired
	private SysUserService sysUserService;
	
	@Autowired
	private SysRoleMenuService sysRoleMenuService;
	
	@Override
	public List<SysMenuEntity> queryListParentId(Long parentId, List<Long> menuIdList) {
		List<SysMenuEntity> menuList = queryListParentId(parentId);
		if(menuIdList == null){
			return menuList;
		}
		
		List<SysMenuEntity> userMenuList = new ArrayList<>();
		for(SysMenuEntity menu : menuList){
			if(menuIdList.contains(menu.getId())){
				userMenuList.add(menu);
			}
		}
		return userMenuList;
	}

	@Override
	public List<SysMenuEntity> queryListParentId(Long parentId) {
		QueryWrapper<SysMenuEntity> queryWrapper = new QueryWrapper<>();
		
		queryWrapper.eq("parent_menu_id", parentId);
		queryWrapper.eq("status", 1);
		
		queryWrapper.orderByAsc("menu_order_num");
		
		return list(queryWrapper);
	}

	@Override
	public List<SysMenuEntity> queryNotButtonList() {
		QueryWrapper<SysMenuEntity> queryWrapper = new QueryWrapper<>();
		
		queryWrapper.ne("menu_type", 2);
		queryWrapper.eq("status", 1);
		
		queryWrapper.orderByAsc("menu_order_num");
		
		return list(queryWrapper);
	}

	@Override
	public List<SysMenuEntity> getUserMenuList(Long userId) {		
		//用户菜单列表
		List<Long> menuIdList = sysUserService.queryAllMenuId(userId);
		return getAllMenuList(menuIdList);
	}

	@Override
	public void delete(Long menuId){
		//删除菜单
		this.removeById(menuId);
		//删除菜单与角色关联
		sysRoleMenuService.removeByMap(new MapUtils().put("menu_id", menuId));
	}

	/**
	 * 获取所有菜单列表
	 */
	private List<SysMenuEntity> getAllMenuList(List<Long> menuIdList){
		//查询根菜单列表
		List<SysMenuEntity> menuList = queryListParentId(0L, menuIdList);
		//递归获取子菜单
		getMenuTreeList(menuList, menuIdList);
		
		return menuList;
	}

	/**
	 * 递归
	 */
	private List<SysMenuEntity> getMenuTreeList(List<SysMenuEntity> menuList, List<Long> menuIdList){
		List<SysMenuEntity> subMenuList = new ArrayList<SysMenuEntity>();
		
		for(SysMenuEntity entity : menuList){
			//目录
			if(entity.getMenuType() == AdminConstants.MenuType.CATALOG.getValue()){
				entity.setList(getMenuTreeList(queryListParentId(entity.getId(), menuIdList), menuIdList));
			}
			subMenuList.add(entity);
		}
		
		return subMenuList;
	}

	@Override
	public SysMenuEntity getByUri(String uri) {
		QueryWrapper<SysMenuEntity> queryWrapper = new QueryWrapper<>();
		
		queryWrapper.eq("menu_url", uri);
		
		return getOne(queryWrapper);
	}
}
